package com.schoolCommunity.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.schoolCommunity.entity.Admin;
import com.schoolCommunity.entity.Information;
import com.schoolCommunity.entity.Member;
import com.schoolCommunity.mapper.AdminMapper;
import com.schoolCommunity.mapper.MemberMapper;
import com.schoolCommunity.service.AdminService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.schoolCommunity.util.CheckInsertAdmin;
import com.schoolCommunity.util.DeleteES;
import com.schoolCommunity.util.InsertES;
import com.schoolCommunity.util.SearchES;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author YTH
 * @since 2020-09-21
 */
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements AdminService {

    @Resource
    private AdminMapper adminMapper;
    @Autowired
    private CheckInsertAdmin checkInsertAdmin;
    @Autowired
    private MemberMapper memberMapper;
    @Autowired
    private SearchES searchES;
    @Autowired
    private DeleteES deleteES;
    @Autowired
    private InsertES insertES;

    @Override
    public Information adminLogin(String email, String password) {

        Information information = new Information();

        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("email", email);
        queryWrapper.eq("password", password);
        queryWrapper.select("email", "id", "real_name", "grade", "phone", "college", "studentNumber");

        Admin myAdmin = adminMapper.selectOne(queryWrapper);

        if (myAdmin != null) {
            information.setMessage("登录成功").setCode(200).setData(myAdmin);
        } else {
            information.setCode(404).setMessage("登录失败");
        }
        return information;
    }

    @Override
    public Information insertAdmin(Admin admin) throws IOException {

        Information information = new Information();

        String result = checkInsertAdmin.CheckInsertMember(admin);

        if (result.equals("数据安全")) {

            int insert = adminMapper.insert(admin);
            if (insert != 0) {
                long l = adminMapper.GetLastId();
                admin.setId(l);
                insertES.InsertAdmin(admin);
                information.setCode(200).setMessage("管理员添加成功");
            } else {
                information.setCode(404).setMessage("管理员添加失败");
            }

        } else {
            information.setCode(404).setMessage(result);
        }

        return information;
    }

    //获取分页普通用户数据
    @Override
    public Information pagingGetAllMember(long count) {

        Information information = new Information();

        Page<Member> page = new Page<>(count, 20);
        memberMapper.selectPage(page, null);
        List<Member> records = page.getRecords();
        if (records.isEmpty()) {
            information.setMessage("不存在页数，没有数据").setCode(404);
        } else {
            information.setMessage("获取第" + count + "页" + records.size() + "条数据成功").setCode(200).setData(records);
        }

        return information;
    }

    //获取最大分页数
    @Override
    public Information getPagingMax() {
        Information information = new Information();

        Page<Member> page = new Page<>(1, 20);
        memberMapper.selectPage(page, null);
        long total = page.getTotal();
        total = total / 20;
        total += 1;

        information.setData(total).setCode(200).setMessage("每页20条，获取最大分页数成功");

        return information;
    }


    //分页获取所有管理员
    @Override
    public Information getAllAdmin(long count) {
        Information information = new Information();

        Page<Admin> page = new Page<>(count, 20);
        IPage<Admin> adminIPage = adminMapper.selectPage(page, null);

        List<Admin> records = adminIPage.getRecords();
        if (records.isEmpty()) {
            information.setMessage("不存在页数，没有数据").setCode(404);
        } else {
            information.setMessage("获取第" + count + "页" + records.size() + "条数据成功").setCode(200).setData(records);
        }

        return information;
    }


    //获取管理员最大分页
    @Override
    public Information getAdminPagingMax() {
        Information information = new Information();

        Page<Admin> page = new Page<>(1, 20);
        adminMapper.selectPage(page, null);
        long total = page.getTotal();
        total = total / 20;
        total += 1;

        information.setData(total).setCode(200).setMessage("每页20条，获取管理员最大分页数成功");

        return information;
    }

    @Override
    public Information deleteAdmin(long deleteId) throws IOException {

        Information information = new Information();

        int i = adminMapper.deleteById(deleteId);
        if (i == 1) {
            information.setCode(200).setMessage("删除id：" + deleteId + "的管理员成功");
            deleteES.DeleteESAdmin(deleteId);
        } else {
            information.setCode(404).setMessage("删除id：" + deleteId + "的管理员失败");
        }
        return information;
    }

    //账号昵称搜索用户
    @Override
    public Information searchMember(String nickname, String familyName, int pageNo) throws IOException {
        Information information = new Information();
        List<Member> memberList = searchES.searchMember(nickname, familyName, pageNo);
        if (!memberList.isEmpty()) {
            information.setData(memberList).setMessage("搜索成功").setCode(200);
        } else {
            information.setData(null).setMessage("没有记录").setCode(404);
        }
        return information;
    }

}
